package com.ruoyix.weblog.controller;

import java.util.List;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import cn.dev33.satoken.annotation.SaIgnore;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;

import jakarta.servlet.http.HttpServletResponse;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyix.common.annotation.RepeatSubmit;
import com.ruoyix.common.annotation.Log;
import com.ruoyix.common.core.controller.BaseController;
import com.ruoyix.common.core.domain.PageQuery;
import com.ruoyix.common.core.domain.R;
import com.ruoyix.common.core.validate.AddGroup;
import com.ruoyix.common.core.validate.EditGroup;
import com.ruoyix.common.core.validate.QueryGroup;
import com.ruoyix.common.enums.BusinessType;
import com.ruoyix.common.utils.poi.ExcelUtil;
import com.ruoyix.weblog.domain.vo.BlogArticleVo;
import com.ruoyix.weblog.domain.bo.BlogArticleBo;
import com.ruoyix.weblog.service.IBlogArticleService;
import com.ruoyix.common.core.page.TableDataInfo;

/**
 * 文章管理
 *
 * @author ruoyi
 * @date 2024-05-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/weblog/article")
public class BlogArticleController extends BaseController {

    private final IBlogArticleService iBlogArticleService;

    /**
     * 查询文章管理列表
     */
    @SaCheckPermission("weblog:article:list")
    @GetMapping("/list")
    @SaIgnore
    public TableDataInfo<BlogArticleVo> list(BlogArticleBo bo, PageQuery pageQuery) {
        return iBlogArticleService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出文章管理列表
     */
    @SaCheckPermission("weblog:article:export")
    @Log(title = "文章管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BlogArticleBo bo, HttpServletResponse response) {
        List<BlogArticleVo> list = iBlogArticleService.queryList(bo);
        ExcelUtil.exportExcel(list, "文章管理", BlogArticleVo.class, response);
    }

    /**
     * 获取文章管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("weblog:article:query")
    @GetMapping("/{id}")
    @SaIgnore
    public R<BlogArticleVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iBlogArticleService.queryById(id));
    }

    @SaIgnore
    @GetMapping("updateLikeCount/{id}")
    public R<Boolean> updateLikeCount(@NotNull(message = "主键不能为空")
                                      @PathVariable Long id){
        return R.ok(iBlogArticleService.updateLikeCount(id));
    }

    @SaIgnore
    @GetMapping("updateViewCount/{id}")
    public R<Boolean> updateViewCount(@NotNull(message = "主键不能为空")
                                      @PathVariable Long id){
        return R.ok(iBlogArticleService.updateViewCount(id));
    }


    /**
     * 新增文章管理
     */
    @SaCheckPermission("weblog:article:add")
    @Log(title = "文章管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BlogArticleBo bo) {
        return toAjax(iBlogArticleService.insertByBo(bo));
    }

    /**
     * 修改文章管理
     */
    @SaCheckPermission("weblog:article:edit")
    @Log(title = "文章管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BlogArticleBo bo) {
        return toAjax(iBlogArticleService.updateByBo(bo));
    }

    /**
     * 删除文章管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("weblog:article:remove")
    @Log(title = "文章管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iBlogArticleService.deleteWithValidByIds(Arrays.asList(ids), true));
    }


    @RequestMapping("/getWebInfoMessage")
    @SaIgnore
    public R<Map<String,String>> getWebInfoMessage(){
        return R.ok(iBlogArticleService.getWebInfoMessage());
    }

    /**
     * 查询文章List
     */
    @SaIgnore
    @RequestMapping ("/getArticlesByType")
    public List<Map<String, Object>> getArticlesByType(@RequestParam(name ="title" ,required = false) String title,@RequestParam(name ="size" ,required = false) int size) {
        return iBlogArticleService.getArticlesByType(title,size);
    }


}
